package com.projet.whattowatch.activity;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.adapter.CinemaAdapter;
import com.projet.whattowatch.objet.Cinema;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class CinemaDetailActivity extends Activity
{
	TextView nomCinema;
	TextView adresseCinema;
	TextView prix2D;
	TextView prix3D;
	Button sendToInternet;
	ViewFlipper viewFlipper;
	private float lastX;
	
	final String EXTRA_CINEMA_NAME = "cinema_name";

	private Cinema cinema;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cinema_layout);
		
		Intent intent = getIntent();
		
		if(intent != null)
		{
			for(int i = 0 ; i < MainActivity.mLibCinema.getListCinema().size(); i++)
			{
				if(MainActivity.mLibCinema.getListCinema().get(i).getNom()
						.equals(intent.getStringExtra(EXTRA_CINEMA_NAME)))
				{
					cinema = MainActivity.mLibCinema.getListCinema().get(i);
				}
			}
		}
		
		nomCinema = (TextView) findViewById(R.id.nomCinema);
		nomCinema.setText(cinema.getNom());
		adresseCinema = (TextView) findViewById(R.id.adresseCinema);
		adresseCinema.setText(cinema.getAdresse());
		prix2D = (TextView) findViewById(R.id.place2D);
		prix2D.setText(String.valueOf(cinema.getPrix2D()) + "€");
		prix3D = (TextView) findViewById(R.id.place3D);
		prix3D.setText(String.valueOf(cinema.getPrix3D()) + "€");
		
		sendToInternet = (Button) findViewById(R.id.website);
		sendToInternet.setOnClickListener(sendListener);
		
		viewFlipper = (ViewFlipper) findViewById(R.id.view_Flipper);
		ArrayList<String> listFilm = new ArrayList<String> ();
		ArrayList<String> listImage = new ArrayList<String> ();
		
		for (int i = 0; i < cinema.listProjection.size(); i++) 
		{
			String[] dateDebut = cinema.listProjection.get(i).getDateDebut().split("-");
			int yearDebut = Integer.parseInt(dateDebut[0]);
			int mouthDebut = Integer.parseInt(dateDebut[1]);
			
			String[] dateFin = cinema.listProjection.get(i).getDateFin().split("-");
			int yearFin = Integer.parseInt(dateFin[0]);
			int mouthFin = Integer.parseInt(dateFin[1]);

			{
				if (yearDebut <= 2014 && yearFin >= 2014)
				{
					if((mouthDebut <= 6 || yearDebut < 2014) && (mouthFin >= 6 || yearFin > 2014))
					{
						for (int j = 0; j < MainActivity.mLibFilm.getListFilm().size(); j++) 
						{
							if ( cinema.listProjection.get(i).getFilm().equals(MainActivity.mLibFilm.getFilm(j).getNom()))
							{
								listFilm.add(MainActivity.mLibFilm.getFilm(j).getNom());
								listImage.add(MainActivity.mLibFilm.getFilm(j).getImageUrl());
							}
						}
					}
				}				
			}
		}
		
		CinemaAdapter movieAdapter = new CinemaAdapter(this, listFilm, listImage);
		
		final int MovieCount = movieAdapter.getCount();
		for (int i = 0; i < MovieCount; i++)
		{
			View item = movieAdapter.getView(i, null, null);
			viewFlipper.addView(item);
		}
	}
	
	public OnClickListener sendListener = new OnClickListener()
	{
		@Override
		public void onClick(View arg0)
		{
			String url = cinema.getUrl();
			Intent sendInternet = new Intent(Intent.ACTION_VIEW);
			sendInternet.setData(Uri.parse(url));
			startActivity(sendInternet);
		}
	};
	
	@Override
	public boolean onTouchEvent(MotionEvent touchevent)
	{
		switch (touchevent.getAction())
		{
			case MotionEvent.ACTION_DOWN:
			{
				lastX = touchevent.getX();
				break;
			}
			
			case MotionEvent.ACTION_UP:
			{
				float currentX = touchevent.getX();

				if (lastX < currentX)
				{
					if (viewFlipper.getDisplayedChild() == 0)
						break;

					viewFlipper.setInAnimation(this, R.anim.in_from_left);
					viewFlipper.setOutAnimation(this, R.anim.out_to_right);
					viewFlipper.showNext();
				}
	
				if (lastX > currentX)
				{
					if (viewFlipper.getDisplayedChild() == 1)
						break;
					
					viewFlipper.setInAnimation(this, R.anim.in_from_right);
					viewFlipper.setOutAnimation(this, R.anim.out_to_left);
					viewFlipper.showPrevious();
				}
				break;
			}
		}
		return false;
	}
}